<?php

require_once 'ActionFramework/action.cls.php';
require_once 'views/all.cls.php';
require_once 'util.php';

class StartAction extends Action {
	
	// No inputs needed
	
	private $message;
	
	public function __construct($message=null) {
		parent::__construct();
		$this->message = $message;
	}
	
	public function trigger($request) {
		// Normal start is login, but if we are logged in, redirect to view_matching
		if ($_SESSION['user'] == null) {
			global $DB;
			list($challenge_id, $challenge) = $DB->create_challenge();
			$view = new StartView($challenge_id, $challenge);
		}
		else if ($_SESSION['user']->admin) {
			$view = r(new AdminAction())->trigger($request);
		}
		else {
			$view = r(new ViewMatchingAction())->trigger($request);
		}
		
		if ($this->message) {
		 	$view->add_message($this->message);
		}
		
		return $view;
	}
	
}
	
?>